<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace addons\video\user\controller;


use \addons\video\model\GiftModel;
/**
 * Description of Gift
 * 直播礼物设置
 * @author zmh
 */
class Gift extends \web\user\controller\AddonUserBase{

    public function index(){
        return $this->fetch();
    }

    /**
     * 礼物列表
     * @return array
     */
    public function loadList(){
        $keyword = $this->_get('keyword');
        $filter = '';
        if ($keyword != null) {
            $filter .= ' and title like \'%' . $keyword . '%\'';
        }
        $order = 'id desc';

        $m = new GiftModel();
        $total = $m->getTotal($filter);
        $rows = $m->getDataList($this->getPageIndex(),$this->getPageSize(), $filter,'',$order);
        return $this->toDataGrid($total, $rows);
    }

    
    public function loadData(){
        $id = $this->_get('id');
        $m = new GiftModel();
        $data = $m->getDetail($id);
        return $data;
    }
    

    public function add_gift(){
        if(IS_POST){
            $data = $_POST;
            try{
                if($data['gift_name']==''){
                    return $this->failData('名称不能为空');
                }
                if($data['gift_amount']==''){
                    return $this->failData('礼物价格不能为空');
                }
                if($data['gift_url']==''){
                    return $this->failData('礼物图片不能为空');
                }
                if($data['profit']>=100){
                    $data['profit'] = 100;
                }
                if($data['profit']<=0){
                    $data['profit'] = 0;
                }
                $data['create_at'] = NOW_DATETIME;
                $data['update_at'] = NOW_DATETIME;
                $m = new GiftModel();
                $res = $m->add($data);
                if($res){
                    return $this->successData('添加成功');
                }else{
                    return $this->failData('添加失败');
                }
            } catch (\Exception $ex) {
                $m->rollback();
                return $this->failData($ex->getMessage());
            }
        }else{
            $this->assign('id','');
            return $this->fetch();
        }
    }


    public function edit(){
        $m = new GiftModel();
        if(IS_POST){
            $data = $_POST;
            if($data['gift_name']==''){
                return $this->failData('名称不能为空');
            }
            if($data['gift_amount']==''){
                return $this->failData('礼物价格不能为空');
            }
            if($data['gift_url']==''){
                return $this->failData('礼物图片不能为空');
            }
            if($data['profit']>=100){
                $data['profit'] = 100;
            }
            if($data['profit']<=0){
                $data['profit'] = 0;
            }
            $data['update_at'] = NOW_DATETIME;
            if($data['id']){
                $m->save($data);
                return $this->successData();
            }else{
                return $this->failData('id为空');
            }
        }else{
            $this->assign('id', $this->_get('id'));
            $this->setLoadDataAction('loadData');
            return $this->fetch();
        }
    }


    /**
     * 逻辑删除
     * @return type
     */
    public function del(){
        $id = $this->_post('id');
        $m = new GiftModel();
        try{
            $ret = $m->deleteData($id);
            if($ret){
                return $this->successData();
            }else{
                return $this->failData($message);
            }
        } catch (\Exception $ex) {
            $m->rollback();
            return $this->failData($ex->getMessage());
        }
    }
    
}
